package com.prayerdiary.adapters;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Locale;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.TextView;

import com.prayerdiary.app.R;
import com.prayerdiary.model.DailyPrayer;
import com.prayerdiary.utils.DeleteBtnVisibilityListener;
import com.prayerdiary.utils.Notifier;

public class CurrentPrayerAdapter extends ArrayAdapter<DailyPrayer>
{
	static class ViewHolder
	{
		public TextView day;

		public TextView month;

		public TextView year;

		public ImageButton fajr;

		public ImageButton zuhr;

		public ImageButton asar;

		public ImageButton maghrib;

		public ImageButton isha;
	}

	private Context context = null;

	private DeleteBtnVisibilityListener delBtnListener = null;

	private ArrayList<DailyPrayer> prayerList;

	private Notifier notifier;

	public CurrentPrayerAdapter(Context context, ArrayList<DailyPrayer> list)
	{
		super(context, R.layout.offered_qaza_item, list);
		this.context = context;
		if (list != null && list.size() > 0) prayerList = list;
	}

	public View getView(int position, View convertView, ViewGroup parent)
	{
		View rowView = convertView;
		final View row = rowView;
		if (rowView == null)
		{
			LayoutInflater inflater = ((Activity) context).getLayoutInflater();
			rowView = inflater.inflate(R.layout.offered_qaza_item, parent, false);

			ViewHolder viewHolder = new ViewHolder();
			viewHolder.day = (TextView) rowView.findViewById(R.id.oqi_day);
			viewHolder.month = (TextView) rowView.findViewById(R.id.oqi_month);
			viewHolder.year = (TextView) rowView.findViewById(R.id.oqi_year);
			viewHolder.fajr = (ImageButton) rowView.findViewById(R.id.oqi_fajr);
			viewHolder.zuhr = (ImageButton) rowView.findViewById(R.id.oqi_zuhr);
			viewHolder.asar = (ImageButton) rowView.findViewById(R.id.oqi_asar);
			viewHolder.maghrib = (ImageButton) rowView.findViewById(R.id.oqi_maghrib);
			viewHolder.isha = (ImageButton) rowView.findViewById(R.id.oqi_isha);
			rowView.setTag(viewHolder);
		}
		if (prayerList != null && prayerList.size() > position)
		{
			final DailyPrayer prayer = prayerList.get(position);
			ViewHolder holder = (ViewHolder) rowView.getTag();

			holder.day.setText(new SimpleDateFormat("dd", Locale.getDefault()).format(prayer.getDate()));
			holder.month.setText(new SimpleDateFormat("MM", Locale.getDefault()).format(prayer.getDate()));
			holder.year.setText(new SimpleDateFormat("yyy", Locale.getDefault()).format(prayer.getDate()));
			if (prayer.getFajr())
			{
				holder.fajr.setBackgroundResource(R.drawable.tick);
			}
			else
			{
				holder.fajr.setBackgroundResource(R.drawable.cross);
			}

			setClickListener(holder.fajr, prayer, "fajr", R.id.oqi_fajr);
			if (prayer.getZuhr())
			{
				holder.zuhr.setBackgroundResource(R.drawable.tick);
			}
			else
			{
				holder.zuhr.setBackgroundResource(R.drawable.cross);
			}
			setClickListener(holder.zuhr, prayer, "zuhr", R.id.oqi_zuhr);
			if (prayer.getAsar())
			{
				holder.asar.setBackgroundResource(R.drawable.tick);
			}
			else
			{
				holder.asar.setBackgroundResource(R.drawable.cross);
			}
			setClickListener(holder.asar, prayer, "asar", R.id.oqi_asar);
			if (prayer.getMaghrib())
			{
				holder.maghrib.setBackgroundResource(R.drawable.tick);
			}
			else
			{
				holder.maghrib.setBackgroundResource(R.drawable.cross);
			}
			setClickListener(holder.maghrib, prayer, "maghrib", R.id.oqi_maghrib);
			if (prayer.getIsha())
			{
				holder.isha.setBackgroundResource(R.drawable.tick);
			}
			else
			{
				holder.isha.setBackgroundResource(R.drawable.cross);
			}
			setClickListener(holder.isha, prayer, "isha", R.id.oqi_isha);
			ImageButton deleteIcon = (ImageButton) rowView.findViewById(R.id.oqi_delete_icon);
			final ImageButton deleteBtn = (ImageButton) rowView.findViewById(R.id.oqi_delete_button);

			if (prayer.isEditMode())
			{
				deleteIcon.setVisibility(View.VISIBLE);
				deleteIcon.setOnClickListener(new OnClickListener()
				{
					@Override
					public void onClick(View v)
					{
						if (deleteBtn.isShown())
						{
							deleteBtn.setVisibility(View.GONE);
							delBtnListener.setVisibleBtnRow(null);
						}
						else
						{

							delBtnListener.setDelBtnVisible(false);
							delBtnListener.setVisibleBtnRow(row);
							deleteBtn.setVisibility(View.VISIBLE);
							deleteBtn.setOnClickListener(new OnClickListener()
							{
								@Override
								public void onClick(View v)
								{
									delBtnListener.setDelBtnVisible(false);
									// deleteBtn.setVisibility(View.GONE);
									notifier.notify(prayer, "DEL");
								}
							});
						}
					}
				});
			}
			else
			{
				deleteBtn.setVisibility(View.GONE);
				deleteIcon.setVisibility(View.GONE);
			}
		}
		return rowView;
	}

	private void setClickListener(final ImageButton btn, final Object o, final String name, final int id)
	{
		btn.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View arg0)
			{
				DailyPrayer p = (DailyPrayer) o;
				if (p.isEditMode()) return;
				if (id == R.id.oqi_fajr)
				{
					if (p.getFajr())
					{
						p.setFajr(false);
					}
					else
					{
						p.setFajr(true);
					}
				}
				else if (id == R.id.oqi_zuhr)
				{
					if (p.getZuhr())
					{
						p.setZuhr(false);
					}
					else
					{
						p.setZuhr(true);
					}
				}
				else if (id == R.id.oqi_asar)
				{
					if (p.getAsar())
					{
						p.setAsar(false);
					}
					else
					{
						p.setAsar(true);
					}
				}
				else if (id == R.id.oqi_maghrib)
				{
					if (p.getMaghrib())
					{
						p.setMaghrib(false);
					}
					else
					{
						p.setMaghrib(true);
					}
				}
				else if (id == R.id.oqi_isha)
				{
					if (p.getIsha())
					{
						p.setIsha(false);
					}
					else
					{
						p.setIsha(true);
					}
				}
				notifier.notify(o, name);
			}
		});
	}

	public void setNofitier(Notifier notifier)
	{
		this.notifier = notifier;
	}

	public void setDeleteBtnVisibilityListener(DeleteBtnVisibilityListener l)
	{
		delBtnListener = l;
	}
}
